package com.example.demo.services.impl;

import com.example.demo.common.AppResult;
import com.example.demo.common.ResultCode;
import com.example.demo.dao.ArticleReplyMapper;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.Article;
import com.example.demo.model.ArticleReply;
import com.example.demo.services.IArticleReplyService;
import com.example.demo.services.IArticleService;
import com.example.demo.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Resource
    private ArticleReplyMapper articleReplyMapper;

    @Resource
    private IArticleService articleService;

    @Override
    public void create(ArticleReply articleReply) {
        //非空校验
        if(articleReply == null || articleReply.getPostUserId() == null
        || articleReply.getPostUserId() <= 0 || articleReply.getArticleId() ==null
                || articleReply.getArticleId() <= 0 || StringUtils.isEmpty(articleReply.getContent())){
            //打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //获取帖子详情
        Article article = articleService.selectDetailsById(articleReply.getArticleId());
        if(article == null || articleReply.getDeleteState() == 1){
            //帖子不存在
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        if(article.getState() == 1){
            //帖子封贴
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_STATE));
        }
        //设置默认值
        articleReply.setLikeCount(0);//点赞数
        articleReply.setState((byte) 0);//状态
        articleReply.setDeleteState((byte) 0);//删除状态
        Date date = new Date();
        articleReply.setCreateTime(date);//创建时间
        articleReply.setUpdateTime(date);//更新时间
        //调用DAO写入回复数据
        int row = articleReplyMapper.insertSelective(articleReply);
        if(row != 1){
            //打印日志
            log.info(ResultCode.ERROR_SERVICES.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //更新帖子回复数
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());//帖子Id
        updateArticle.setReplyCount(article.getReplyCount() + 1);//回复数
        updateArticle.setUpdateTime(new Date());//更新时间
        //调用帖子的更新方法
        articleService.updateById(updateArticle);
        //打印日志
        log.info("回复成功，article id = " + article.getId());
    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        //非空校验
        if(articleId == null || articleId <= 0){
            //打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验主贴是否存在
        Article article = articleService.selectDetailsById(articleId);
        if (article == null || article.getDeleteState() == 1){
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //调用DAO
        List<ArticleReply> articleReplies = articleReplyMapper.selectByArticleId(articleId);
        //返回结果集
        return articleReplies;
    }
}
